;(function ($) {
    var receive_coupon = 'receive_coupon';

    /**
     * 获取可领取的优惠券列表
     */
    $.getReceiveCouponList = function () {
        setTimeout(function () {
            var receiveCoupon = getCookie(receive_coupon);
            //没有cookie缓存
            if(receiveCoupon ==null){
                findForUsersCouponList();
            }
        },150);
    }

    /**
     * 获取可领取的优惠券列表
     */
    $.findCouponList = function (status) {
        findForUsersCouponList(status);
    }

    /**
     * 获取用户可以领取的优惠券
     */
    function findForUsersCouponList(status) {
        $.ajax({
            url: baseloaction + '/mobile/uc/op/coupon/findForUsersCouponList',
            type:'post',
            dataType:'json',
            success:function (result) {
                if(result.success==true){
                    var couponList = result.entity;
                    if(couponList !=null && couponList.length > 0){
                        //优惠券弹框
                        popCardFn(couponList);
                        $(".coupon-box-btn").click(function () {
                            var _this = $(this);
                            var couponId = _this.data('coupon');
                            //领取优惠券
                            userReceiveCoupon(couponId);
                        });
                    }else{
                        if(status==true){
                            $.toast('没有可领取的优惠券');
                        }
                    }
                }else{
                    if(status==true){
                        $.toast(result.message);
                    }
                }
            }
        });
    }

    /**
     * 优惠券弹框
     */
    function popCardFn(couponList) {
        var _ele = '<div class="popCardEle__wrap">';
        _ele +='<header class="popCardEle-head"></header>';
        _ele +='<section class="popCardEle-content swiper-container swiper-container-banner" data-space-between=\'0\' id="infinite-scroll-bx">';
        _ele +='<ul id="coupon-list-box" class="swiper-wrapper" style="height: 100%;">';
        $.each(couponList,function (index,coupon) {
            _ele +='<li class="swiper-slide">';
            _ele +='<div class="cards-yh-bx">';
            _ele +='<div class="row no-gutter">';
            _ele +='<section class="col-33">';
            _ele +='<div class="text-center Coupon-red-bg">';
            _ele +='<div>';
            if(coupon.type==1){
                _ele +='<span class="fs7">折</span>';
            }else{
                _ele +='<span class="fs7">￥</span>';
            }
            _ele +='<span class="uAccount-number">';
            _ele +='<b>5.0</b>';
            _ele +='</span>';
            _ele +='</div>';
            _ele +='<div>';
            if(coupon.type==1){
                _ele +='<span class="fs6">折扣券</span>';
            }else{
                _ele +='<span class="fs6">立减券</span>';
            }
            _ele +='</div></div></section>';
            _ele +='<section class="col-66">';
            _ele +='<div class="cards-yh-r-content">';
            _ele +='<section class="fs7 ellipsis-rows">';
            _ele +='<span class="c-333">消费满</span>';
            _ele +='<span class="c-red">'+coupon.minAmount+'</span>';
            _ele +='<span class="c-333">元时可用</span>';
            _ele +='</section>';
            _ele +='<div class="clearfix dashed">';
            if(coupon.timeType==2){
                _ele +='<span class="pull-left fs4 c-888">有效期至永远</span>';
            }else{
                _ele +='<span class="pull-left fs4 c-888">有效期至'+coupon.endTime+'</span>';
            }
            _ele +='<aside class="pull-right">';
            _ele +='<a href="javascript:void(0);" data-coupon="'+coupon.id+'" class="button button-fill button-round button-danger fs4 coupon-box-btn">领取</a>';
            _ele +='</aside></div></div></section></div></div>';
            _ele +='</li>';
        });
        _ele +='</ul></section></div>';
        $.confirm(_ele,
            function () {
                var couponBox = $(".coupon-box-btn");
                var couponIds = couponBox.map(function () {
                    return $(this).data('coupon');
                }).get().join(",");
                //领取优惠券
                userReceiveCoupon(couponIds);
            },
            function () {
                SetCookieOutTime(receive_coupon,true,"d1");
                $('.modal.modal-in').addClass('popCardEle-bx');
            }
        );
        $('.modal.modal-in').addClass('popCardEle-bx')
        $('.popCardEle-bx .modal-buttons > .modal-button:first-child').text('有钱任性,不需要')
        $('.popCardEle-bx .modal-buttons > .modal-button:last-child').text('全部领取')
        $('.popCardEle-bx.modal').css({
            'margin-top': 0,
            'top': 0,
            '-webkit-transform': 'translateY(50%)',
            'transform': 'translateY(50%)'
        });
        //初始化上下滚动
        var popCardEle = new Swiper(".popCardEle-content", {
            autoplay: false,
            direction: 'vertical',
            loop: 0,
            freeMode : false,
            freeModeSticky: true,
            slidesPerView : 3,
            spaceBetween : 10,
            slidesOffsetBefore : 5,
            slidesOffsetAfter: 5
        });
    }

    /**
     * 领取优惠券
     * @param couponIds 领取的优惠ID串
     */
    function userReceiveCoupon(couponIds) {
        $.ajax({
            url: baseloaction + '/mobile/uc/op/coupon/couponReceive',
            type:'post',
            dataType:'json',
            data:{
                'couponIds': (couponIds +'')
            },
            success:function (result) {
                $.toast(result.message);
                if(result.success==true){
                    SetCookieOutTime(receive_coupon,true,"d1");
                    $('.modal.modal-in').addClass('popCardEle-bx');
                }
            }
        });
    }
}(Zepto));
